import java.util.concurrent.Future;

/**
 * @author zhangdi
 * @version 1.0
 * @date 2022.3.7 17:22
 */
public abstract class TestTaskBase implements TestTask {
    String mName;
    TaskState state = TaskState.IDLE;
    Future<?> future;

    public TestTaskBase(String mName) {
        this.mName = mName;
    }

    @Override
    public void stop() {
        state = TaskState.STOP;
        if(!future.isCancelled()){
            future.cancel(true);
        }
        System.out.println(mName + "-退出");
    }

    @Override
    public void run() {
        System.out.println(getClass().getSimpleName() + "111");
        int index = 0;
//        while (true) {
            if(state != TaskState.RUNNING) {
                return;
            }

//            try {
//                Thread.sleep(1000);
                System.out.println(mName+"- 执行");
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
    }

    @Override
    public TaskState getState() {
        return state;
    }

    @Override
    public String getName() {
        return mName;
    }

    @Override
    public void future(Future<?> future) {
        state = TaskState.RUNNING;
        this.future = future;
    }
}
